/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.model;


import java.util.ArrayList;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.dao.GenericDAO;

import scheduler.databean.Course;
import scheduler.databean.Section;
import scheduler.formbean.searchForm;

public class CourseDaoBeanFactoryImpl
extends GenericDAO<Course> 
implements CourseDao { 

	public CourseDaoBeanFactoryImpl(String dbDriver, String dbUrl) {
		this(BeanTable.getSQLInstance(Course.class, "course", dbDriver, dbUrl));
	}

	private CourseDaoBeanFactoryImpl(BeanTable<Course> table) {
		super(table, "id");
		getTable().setIdleConnectionCleanup(true);
	}

	public Course create(int major, int level, String courseName, String description, int unit)
	throws DAOException {
		Course newCourse = new Course(major*1000+level);
		newCourse.setMajorNumber(major);
		newCourse.setLevelNumber(level);
		newCourse.setCourseName(courseName);
		newCourse.setDescription(description);
		newCourse.setUnit(unit); 

		newCourse= create(newCourse);

		return newCourse;
	}


	public Course[] getCoursesBySections(Section[] sec) throws DAOException{
		ArrayList<Course> courses= new ArrayList<Course>();

		for(int i=0; i< sec.length; i++)
		{
			boolean add=true;
			try {
				Course temp=getFactory().lookup(sec[i].getCourseId());
				if(temp!=null){
					for(int x=0; x<courses.size(); x++)
					{
						if(courses.get(x).getId()==
							temp.getId())
							add=false;
					}
					if(add){
						courses.add(temp);
					}
				}
			} catch (RollbackException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Course[] coursearray= new Course[courses.size()];
		for(int i=0; i<courses.size(); i++)
		{
			coursearray[i]=courses.get(i);
		}
		return coursearray;
	}

	public Course[] getSearch(searchForm form) throws DAOException {
		try {

			MatchArg matchlevel= 	MatchArg.greaterThan("id",0);
			MatchArg matchMajor=	MatchArg.greaterThan("id",0);
			MatchArg CourseName= 	MatchArg.greaterThan("id",0);
			MatchArg Description= 	MatchArg.greaterThan("id",0);
			MatchArg Unit= 			MatchArg.greaterThan("id",0);

			if(form.getNumber()>0){
				int level, major =-1;
				if(form.getNumber()>=1000)
				{
					major=form.getNumber()/1000;
					level=form.getNumber()%1000;

					matchlevel= MatchArg.equals("levelNumber",level);
					matchMajor= MatchArg.equals("majorNumber", major);            		

				}
				else if(form.getNumber()>99){
					level=form.getNumber();
					matchlevel= MatchArg.equals("levelNumber",level);            
				}
				else if(form.getNumber()>0){
					major=form.getNumber();  
					matchMajor= MatchArg.equals("majorNumber", major);             		
				}   		

			}
			if(form.getCourseName()!=null && form.getCourseName().length()!=0){
				CourseName= MatchArg.containsIgnoreCase("courseName", form.getCourseName());        		
			}

			if(form.getDescription()!=null && form.getDescription().length()!=0){
				Description= MatchArg.containsIgnoreCase("description", form.getDescription());   
			}     	

			if(form.getUnits()>0){
				Unit= MatchArg.equals("unit", form.getUnits());   
			}  


			return getFactory().match(matchlevel,matchMajor,CourseName,Description, Unit);

		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}




	public Course lookup(int id) throws DAOException {
		try {
			return getFactory().lookup(id);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}




}
